﻿app.controller('SafetyCultureController', function ($scope, $http, $window, $compile, $filter, ipCookie, common, option, Upload) {
    $scope.result = {};
    $scope.dateOption = option.date;
    $scope.datetimeOption = option.datetime;

    $scope.GetCaseTypeList = function () {
        $scope.CaseTypeList = [];
        $http({
            method: 'POST',
            url: '../SafetyCulture/GetSafetyCultureConfig',
            data: {"Type":1}
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.CaseTypeList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }


    $scope.GetCaseSourceList = function () {
        $scope.CaseSourceList = [];
        $http({
            method: 'POST',
            url: '../SafetyCulture/GetSafetyCultureConfig',
            data: { "Type": 2 }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.CaseSourceList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.AddSafetyCaseForm = function (model) {
        if (model.CaseTitle == null || model.CaseTitle == "") {
            alertify.alert("请填写标题");
            return false;
        }
        var editorInstance = CKEDITOR.instances['casecontent'];
        model.CaseContent = editorInstance.getData();
        var files = document.getElementById("taskfile").files;

        $scope.Submiting = true;
        $scope.LoadingMsg = "正在提交,请稍后...";
        Upload.upload({
            method: 'POST',
            url: '../SafetyCulture/AddSafetyCaseForm',
            data: { 'model': model},
            file: files
        })
            .success(function (ret) {
                $scope.Submiting = false;
                if (ret.Code == 100) {
                    alertify.alert(ret.Msg);
                    setTimeout(function () {
                        window.location.href = "SafetyCasePageList?page=1";
                    }, 2000);
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })
    }
    $scope.EditSafetyCaseForm = function (model,page) {
        if (model.CaseTitle == null || model.CaseTitle == "") {
            alertify.alert("请填写标题");
            return false;
        }
        var editorInstance = CKEDITOR.instances['casecontent'];
        model.CaseContent = editorInstance.getData();
        var files = document.getElementById("taskfile").files;

        $scope.Submiting = true;
        $scope.LoadingMsg = "正在提交,请稍后...";
        Upload.upload({
            method: 'POST',
            url: '../SafetyCulture/EditSafetyCaseForm',
            data: { 'model': model },
            file: files
        })
            .success(function (ret) {
                $scope.Submiting = false;
                if (ret.Code == 100) {
                    alertify.alert(ret.Msg);
                    setTimeout(function () {
                        window.location.href = "SafetyCasePageList?page="+page;
                    }, 2000);
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })
    }
    $scope.GetSafetyCasePageList = function (model, page) {
        $scope.DataLoading = true;
        $scope.DataList = [];
        $http({
            method: 'POST',
            url: '../SafetyCulture/GetSafetyCasePageList',
            data: { "model": model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.DataList = ret.Data.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.DeleteSafetyCase = function (Id, model, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除该数据？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../SafetyCulture/DeleteSafetyCase',
                    data: { 'Id': Id },
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            alertify.alert(ret.Msg);
                            setTimeout(function () {
                                $scope.GetSafetyCasePageList(model, page);
                            }, 2000);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }, function (ev) {
                ev.preventDefault();

            });
    }

    $scope.GetSafetyCaseDetail = function (Id,v) {
        $scope.DataLoading = true;
        $scope.Contruction = {};
        $http({
            method: 'POST',
            url: '../SafetyCulture/GetSafetyCaseDetail',
            data: { "Id": Id },
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.DataModel = ret.Data;
                    if (v) {
                        $("#casecontent").html($scope.DataModel.CaseContent);
                    }
                    else {
                        var editorInstance = CKEDITOR.instances['casecontent'];
                        editorInstance.setData($scope.DataModel.CaseContent);
                    }

                    $scope.DataModel.CaseTypeId = $scope.DataModel.CaseTypeId + "";
                    $scope.DataModel.CaseSourceId = $scope.DataModel.CaseSourceId + "";
                    $scope.DataModel.CaseDate = $filter('JSONDate')($scope.DataModel.CaseDate, 'yyyy-MM-dd');
                    $scope.DataModel.CreateTime = $filter('JSONDate')($scope.DataModel.CreateTime, 'yyyy-MM-dd HH:mm:ss');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }
    $scope.DeleteSafetyCultureFile = function (model, list) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除该文件？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../SafetyCulture/DeleteSafetyCultureFile',
                    data: { 'FileId': model.FileId },
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            $scope.AlertBox(ret.Msg);
                            let index = list.indexOf(model);
                            list.splice(index, 1);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }, function (ev) {
                ev.preventDefault();

            });
    }


    //////////////////安全奖惩////////////////////////////
    $scope.ImportEmployeeList = function (formno) {
        var files = document.getElementById("employeelistfile").files;
        if (files.length == 0) {
            alertify.alert("请选择文件");
            return false;
        }
        $scope.Submiting = true;
        $scope.LoadingMsg = "正在提交,请稍后...";
        Upload.upload({
            method: 'POST',
            url: '../SafetyCulture/ImportSafetyRapItem',
            data: { 'FormNo': formno },
            file: files
        })
            .success(function (ret) {
                $scope.Submiting = false;
                if (ret.Code == 100) {
                    alertify.alert(ret.Msg);
                    $scope.ItemList = ret.Data;
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })
    }

    $scope.AddSafetyRapForm = function (model,items,pagename) {
        var editorInstance = CKEDITOR.instances['casecontent'];
        model.NoticeContent = editorInstance.getData();
        var files = document.getElementById("docfiles").files;

        $scope.Submiting = true;
        $scope.LoadingMsg = "正在提交,请稍后...";
        Upload.upload({
            method: 'POST',
            url: '../SafetyCulture/AddSafetyRapForm',
            data: { 'model': model, "Items": items },
            file: files
        })
            .success(function (ret) {
                $scope.Submiting = false;
                if (ret.Code == 100) {
                    alertify.alert(ret.Msg);
                    setTimeout(function () {
                        window.location.href = pagename+"?page=1";
                    }, 2000);
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })
    }

    $scope.GetSafetyRapPageList = function (model, page) {
        $scope.DataLoading = true;
        $scope.DataList = [];
        $http({
            method: 'POST',
            url: '../SafetyCulture/GetSafetyRapPageList',
            data: { "model": model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.DataList = ret.Data.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.RemoveRapItems = function (item, list) {
        let index = list.indexOf(item);
        list.splice(index, 1);
    }

    $scope.DeleteSafetyRap = function (Id, model, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除该数据？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../SafetyCulture/DeleteSafetyRap',
                    data: { 'Id': Id },
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            $scope.AlertBox(ret.Msg);
                            setTimeout(function () {
                                $scope.GetSafetyRapPageList(model, page);
                            }, 2000);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }, function (ev) {
                ev.preventDefault();

            });
    }

    $scope.GetSafetyRapDetail = function (Id, v) {
        $scope.DataLoading = true;
        $scope.Contruction = {};
        $http({
            method: 'POST',
            url: '../SafetyCulture/GetSafetyRapDetail',
            data: { "Id": Id },
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.DataModel = ret.Data;
                    if (v) {
                        $("#casecontent").html($scope.DataModel.NoticeContent);
                    }
                    else {
                        var editorInstance = CKEDITOR.instances['casecontent'];
                        editorInstance.setData($scope.DataModel.NoticeContent);
                    }
                    $scope.DataModel.ReportTime = $filter('JSONDate')($scope.DataModel.ReportTime, 'yyyy-MM-dd');
                    $scope.DataModel.CreateTime = $filter('JSONDate')($scope.DataModel.CreateTime, 'yyyy-MM-dd HH:mm:ss');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.DeleteRapItem = function (model, list) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除该文件？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../SafetyCulture/DeleteRapItem',
                    data: { 'Id': model.Id },
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            $scope.AlertBox(ret.Msg);
                            let index = list.indexOf(model);
                            list.splice(index, 1);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }, function (ev) {
                ev.preventDefault();

            });
    }


    $scope.UpdateRapItem = function (model) {
        model.CreateTime = $filter('JSONDate')(model.CreateTime, 'yyyy-MM-dd HH:mm:ss');
        $http({
            method: 'POST',
            url: '../SafetyCulture/UpdateRapItem',
            data: { 'model': model },
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })
    }

    $scope.EditSafetyRapForm = function (model, items, pagename) {
        var editorInstance = CKEDITOR.instances['casecontent'];
        model.NoticeContent = editorInstance.getData();
        var files = document.getElementById("docfiles").files;

        $scope.Submiting = true;
        $scope.LoadingMsg = "正在提交,请稍后...";
        Upload.upload({
            method: 'POST',
            url: '../SafetyCulture/EditSafetyRapForm',
            data: { 'model': model, "Items": items },
            file: files
        })
            .success(function (ret) {
                $scope.Submiting = false;
                if (ret.Code == 100) {
                    alertify.alert(ret.Msg);
                    setTimeout(function () {
                        window.location.href = pagename + "?page=1";
                    }, 2000);
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
                $scope.PageLoading = false;
                alertify.alert("请检查网络");
            })
    }
});